<?php
namespace Main\Controller;
use Think\Controller;
class IndexController extends AppController {

    function __construct()
    {
        parent::__construct();
        $this->M_company = M('Company');
        $this->M_role = M('Role');
        $this->M_account = M('Account');
    }

    public function index(){
        $this->display();
    }

    public function user_list(){
        // $whe = '1=1';
        // $whe .= ' and a.is_son = 0';
        $whe['is_son'] = 0;
        $where = $this->get_where($whe);
        $count = $this->M_company->where($where)->count();
        $page = $this->page($count);
        $list = $this->M_company->join('as c left join sp_account as a ON c.c_id = a.c_id')->where($where)->field('c.*,a.account_name')->limit($page['f'],$page['l'])->select();
        $this->assign('list',$list);
        $this->assign('title','所有商家');
        $this->display();
    }


    public function info(){
        $uid = I('get.id',0,'intval');
        $package = $this->M_role->select();//套餐
        $this->assign('package',$package);
        if($uid){
            $info = $this->M_company->join('as c left join sp_account as a ON c.c_id = a.c_id')->where('c.c_id = '.$uid)->find();
            // echo "<pre>";
            // var_dump($info);exit;
            $this->assign('info',$info);
        }
        if(IS_POST){
            $company['role_id'] = I('post.role_id');
            $company['company_name'] = I('post.company_name');
            $company['add_time'] = time();
            $company['tel'] = I('post.tel');
            $company['full_name'] = I('post.full_name');
            $company['expire_time'] = strtotime(I('post.expire_time'));
            $account['account_name'] = I('post.account_name');
            $account['role_id'] = I('post.role_id');
            if(I('post.password')){
                $account['password'] = md5(I('post.password'));
            }
            $upload = new \Think\Upload();
            $upload->maxSize = 3145728 ;
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
            $upload->rootPath = './';
            $upload->savePath = './Uploads/'.$this->c_id.'/company_logo/'.$this->uid.'/';
            $upload->saveName = 'my_filename';
            $img_info   =   $upload->upload();
            if ($img_info) {
                $company['company_logo'] = $img_info['company_logo']['savepath'].$img_info['company_logo']['savename'];
            }
            //添加商户
            if($uid){
                $this->M_company->where('c_id ='.$uid)->save($company);
                $this->M_account->where('c_id ='.$uid)->save($account);
            }else{
                $result = $this->M_company->add($company);
                $account['c_id'] = $result;
                $this->M_account->add($account);
            }
            die($this->success('修改成功！',U('index/user_list')));
        }
        $this->display();
    }

    //状态
    public function is_status(){
        if(!IS_AJAX)
            die($this->ajaxReturn('对不起，您不是ajax访问'));
        $id = I('get.id',0,'intval');
        if(!$id)
            die($this->ajaxReturn('参数错误！'));
        $info = $this->M_company->find($id);
        $is_status = $info['is_status']?0:1;
        $this->M_company->is_home = $is_status;
        $this->M_company->save();
        $is_status?$this->ajaxReturn(array('result'=>'yes')):$this->ajaxReturn(array('result'=>'no'));
    }
}